﻿using Newtonsoft.Json;
using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Server.Util
{
    public class CacheUtil
    {
        public string DBServer = "192.168.1.238";
        public string CachePrefix = "NetInvoice";
        public void CacheSet(string key, object data, int expired = 0)
        {
            try
            {
                RedisClient redis = new RedisClient(DBServer, 8899);
                if (expired == 0)
                    redis.Set(CachePrefix + "|" + key, JsonConvert.SerializeObject(data));
                else
                    redis.Set(CachePrefix + "|" + key, JsonConvert.SerializeObject(data), DateTime.Now.AddSeconds(expired));
            }
            catch (Exception)
            {
                throw;
            }
        }
        public object CacheGet(string key)
        {
            try
            {
                RedisClient redis = new RedisClient(DBServer, 8899);
                string result = redis.Get<string>(CachePrefix + "|" + key);
                return result;
            }
            catch (Exception)
            {
                throw;
            }

        }
    }
}
